/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.cspimp.esrz.service.impl;

import me.zhengjie.modules.cspimp.esrz.domain.SclItem;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.cspimp.esrz.repository.SclItemRepository;
import me.zhengjie.modules.cspimp.esrz.service.SclItemService;
import me.zhengjie.modules.cspimp.esrz.service.dto.SclItemDto;
import me.zhengjie.modules.cspimp.esrz.service.dto.SclItemQueryCriteria;
import me.zhengjie.modules.cspimp.esrz.service.mapstruct.SclItemMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://docs.auauz.net
* @description 服务实现
* @author 黄星星
* @date 2020-06-26
**/
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class SclItemServiceImpl implements SclItemService {

    private final SclItemRepository SclItemRepository;
    private final SclItemMapper SclItemMapper;

    @Override
    public Map<String,Object> queryAll(SclItemQueryCriteria criteria, Pageable pageable){
        Page<SclItem> page = SclItemRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(SclItemMapper::toDto));
    }

    @Override
    public List<SclItemDto> queryAll(SclItemQueryCriteria criteria){
        return SclItemMapper.toDto(SclItemRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public SclItemDto findById(Long id) {
        SclItem SclItem = SclItemRepository.findById(id).orElseGet(SclItem::new);
        ValidationUtil.isNull(SclItem.getId(),"SclItem","id",id);
        return SclItemMapper.toDto(SclItem);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SclItemDto create(SclItem resources) {
        return SclItemMapper.toDto(SclItemRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SclItem resources) {
        SclItem SclItem = SclItemRepository.findById(resources.getId()).orElseGet(SclItem::new);
        ValidationUtil.isNull( SclItem.getId(),"SclItem","id",resources.getId());
        SclItem.copy(resources);
        SclItemRepository.save(SclItem);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            SclItemRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<SclItemDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SclItemDto SclItem : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("所属公司", SclItem.getCid());
            map.put("所属项目", SclItem.getPid());
            map.put("创建时间", SclItem.getCreateTime());
            map.put("更新时间", SclItem.getUpdateTime());
            map.put("创建者", SclItem.getCreateBy());
            map.put("更新者", SclItem.getUpdateBy());
            map.put("名称", SclItem.getItemName());
            map.put("危害或者潜在事件", SclItem.getHazardOrPotentialEvent());
            map.put("产生偏差的主要后果", SclItem.getMainConsequencesDeviations());
            map.put("现有安全控制措施", SclItem.getExistingSafetyControlMeasures());
            map.put("建议改正/控制措施", SclItem.getControlMeasures());
            map.put("分析方法", SclItem.getAnalyticalMethod());
            map.put("事件发生可能性主键", SclItem.getItemHappenId());
            map.put("事件发生可能性名称", SclItem.getItemHappenName());
            map.put("暴露于危险环境的频繁程度主键", SclItem.getItemEId());
            map.put("暴露于危险环境的频繁程度名称", SclItem.getItemEName());
            map.put("发生事故事件偏差产生的后果严重性主键", SclItem.getItemCId());
            map.put("发生事故事件偏差产生的后果严重性名称", SclItem.getItemCName());
            map.put("R（风险值=L*E*C", SclItem.getItemR());
            map.put("风险等级主键", SclItem.getItemRiskId());
            map.put("风险等级code", SclItem.getItemRiskCode());
            map.put("风险等级名称", SclItem.getItemRiskName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}